﻿using System;
using System.Linq.Expressions;
using CompactMapper.Orm.Enumerations;

namespace CompactMapper.Orm.Queries
{
    internal class EnumEqualsCriterion<T, TEnumVal, TBackingVal> : Criterion<T, TBackingVal>
        where TEnumVal : Enumeration<TBackingVal>
    {
        private readonly Expression<Func<T, TEnumVal>> _columnExpression;

        public EnumEqualsCriterion(Expression<Func<T, TEnumVal>> columnExpression, TBackingVal targetValue)
            : base(((MemberExpression) columnExpression.Body).Member.Name, targetValue)
        {
            _columnExpression = columnExpression;
        }

        public override CriterionType Type
        {
            get { return CriterionType.EnumEquals; }
        }


        public override bool Validate(T entity)
        {
            return _columnExpression.Compile().Invoke(entity).Equals(TargetValue);
        }
    }
}